home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / SNNSV32.ZIP / SNNSv3.2 / xgui / sources / ui_utilP.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-25  |  10.2 KB  |  382 lines

  1. /*****************************************************************************
  2.   FILE           : ui_utilP.c
  3.   SHORTNAME      : ui_utilP.ph
  4.   SNNS VERSION   : 3.2
  5.  
  6.   PURPOSE        : 
  7.   NOTES          :
  8.  
  9.   AUTHOR         : Tilman Sommer 
  10.   DATE           : 18.5.1990
  11.  
  12.   CHANGED BY     : Sven Doering
  13.   IDENTIFICATION : @(#)ui_utilP.c    1.9 3/2/94
  14.   SCCS VERSION   : 1.9
  15.   LAST CHANGE    : 3/2/94
  16.  
  17.              Copyright (c) 1990-1994  SNNS Group, IPVR, Univ. Stuttgart, FRG
  18.  
  19. ******************************************************************************/
  20.  
  21.  
  22. #include <stdio.h>
  23.  
  24. #include "ui.h"
  25.  
  26. #include <X11/Xaw/Text.h>
  27. #include <X11/Xaw/TextSrc.h>
  28. #include <X11/Xaw/AsciiText.h>
  29.  
  30. #include "ui_display.h"
  31. #include "ui_mainP.h"
  32. #include "ui_fileP.h"
  33. #include "ui_netGraph.h"
  34. #include "ui_textP.h"
  35. #include "ui_action.h"
  36.  
  37. #include "kr_ui.h"
  38.  
  39. #include "ui_utilP.ph"
  40.  
  41.  
  42. /*****************************************************************************
  43.   FUNCTION : ui_isUnitVisibleInAnyDisplay
  44.  
  45.   PURPOSE  : checks, whether the specified unit ist visible in any display
  46.   NOTES    :
  47.  
  48.   UPDATE   : 1.2.1990
  49. ******************************************************************************/
  50.  
  51. Bool ui_isUnitVisibleInAnyDisplay(int unitNo)
  52. {
  53.     struct Ui_DisplayType *dPtr;
  54.     
  55.     dPtr = ui_displ_listPtr;
  56.     while (dPtr != NULL) {
  57.     if ((krui_getUnitSubnetNo(unitNo) == dPtr->subNetNo) AND
  58.         (krui_getUnitLayerNo(unitNo) BIT_AND dPtr->layers))
  59.         return(TRUE);
  60.     dPtr = dPtr->nextPtr;
  61.     }
  62.     return(FALSE);
  63. }
  64.  
  65.  
  66. /*****************************************************************************
  67.   FUNCTION : ui_checkError
  68.  
  69.   PURPOSE  : checks the error value, coming from a kernel function call.
  70.              If an error has occured, ui gets the message from the kernel
  71.          and writes it to the text window.
  72.   NOTES    :
  73.  
  74.   UPDATE   : 24.8.1990
  75. ******************************************************************************/
  76.  
  77. void
  78. ui_checkError(krui_err error)
  79. {
  80.     if ((ui_kernelError = error) != 0)
  81.     ui_tw_errorMessage(krui_error(error));
  82. }
  83.  
  84.  
  85.  
  86.  
  87. /*****************************************************************************
  88.   FUNCTION : ui_krui_sitesExistInNetwork
  89.  
  90.   PURPOSE  : returns TRUE, if there is at least one site in the network
  91.   NOTES    : Only the site table is checked. If there is no unit with sites,
  92.              but there is at least one site table entry, this function still
  93.              returns TRUE!
  94.   UPDATE   : 26.8.1990
  95. ******************************************************************************/
  96.  
  97. Bool
  98. ui_krui_sitesExistInNetwork(void)
  99. {
  100.     char *name, *funcName;
  101.  
  102.     return(krui_getFirstSiteTableEntry(&name, &funcName));
  103. }
  104.  
  105.  
  106. /*****************************************************************************
  107.   FUNCTION : ui_krui_setCurrentSite
  108.  
  109.   PURPOSE  : sets the current site
  110.   NOTES    : The unit must exist (no error checking) !!
  111.              If the unit has no sites, only the current unit is set and TRUE will
  112.          be returned.
  113.   UPDATE   : 26.8.1990
  114. ******************************************************************************/
  115.  
  116. Bool
  117. ui_krui_setCurrentSite(int targetNo, char *siteName)
  118. {
  119.     (void) krui_setCurrentUnit(targetNo);    /* target */
  120.     if (krui_getUnitInputType(targetNo) != SITES)
  121.     return(TRUE);
  122.  
  123.     if ((siteName != NULL) AND strlen(siteName)) 
  124.     return((krui_setSite(siteName) == UI_SET_OK));
  125.     else
  126.     return(FALSE); /* invalid site name */
  127. }
  128.  
  129.  
  130.  
  131. /*****************************************************************************
  132.   FUNCTION : ui_krui_setCurrentLink
  133.  
  134.   PURPOSE  : sets the current link (kernel)
  135.   NOTES    : This routine was written, because the handling of the current
  136.              link is hard sometimes, specially while doing the loops
  137.          getFirstSuccUnit..getNextSuccUnit and
  138.          getFirstPredUnit..getNextPredUnit
  139.          The link and all units must exist (no error checking) !!
  140.  
  141.   UPDATE   : 26.8.1990
  142. ******************************************************************************/
  143.  
  144. void
  145. ui_krui_setCurrentLink(int sourceNo, int targetNo, char *siteName)
  146. {
  147.     (void) krui_setCurrentUnit(targetNo);    /* target */
  148.     (void) ui_krui_setCurrentSite(targetNo, siteName);
  149.     (void) krui_isConnected(sourceNo);       /* sets current link */
  150. }
  151.  
  152.  
  153. /*****************************************************************************
  154.   FUNCTION : ui_correctName
  155.  
  156.   PURPOSE  : replace all ' ', '\t' und '\n' with underscores ('_') 
  157.   NOTES    :
  158.  
  159.   UPDATE   : 27.9.1990
  160. ******************************************************************************/
  161.  
  162. void ui_correctName(char *string)
  163. {
  164.     int j;
  165.     if (string == NULL) return;
  166.     /* replace white space with underscores */
  167.     for (j=0; string[j] != '\0'; j++) {
  168.     if ((string[j] == ' ') OR
  169.         (string[j] == '\t') OR
  170.         (string[j] == '\n'))
  171.         string[j] = '_';
  172.     }
  173. }
  174.  
  175. /*****************************************************************************
  176.   FUNCTION : ui_cutTrailingZeros
  177.  
  178.   PURPOSE  : replace all trailing '0' (except after '.') 
  179.              with null character ('\0') 
  180.   NOTES    :
  181.  
  182.   UPDATE   : 27.9.1990
  183. ******************************************************************************/
  184.  
  185. void ui_cutTrailingZeros(char *string)
  186. {
  187.     int j;
  188.     if ((string == NULL) OR (strlen(string) < 2)) return;
  189.     for (j = strlen(string)-1 ; ((j > 0) AND (string[j] != '.')) ; j--) {
  190.     if ((string[j] == '0') AND (string[j-1] != '.'))
  191.         string[j] = '\0';
  192.     }
  193. }
  194.  
  195.  
  196.  
  197. /*****************************************************************************
  198.   FUNCTION :                     ui_utilGridToPix
  199.  
  200.   PURPOSE  : calculates the center pixel coordinates of a unit grid position.
  201.   RETURNS  : pixel coordinate
  202.   NOTES    : see above
  203. *****************************************************************************/
  204.  
  205. struct PosType
  206. ui_utilGridToPix(struct Ui_DisplayType *displayPtr, struct PosType gridPos)
  207. {
  208.     struct PosType  pixPos;
  209.  
  210.     pixPos.x = displayPtr->gridSize * (gridPos.x - displayPtr->origin.x) + 
  211.     displayPtr->gridSize/2;
  212.     pixPos.y = displayPtr->gridSize * (gridPos.y - displayPtr->origin.y) + 
  213.     displayPtr->gridSize/2;
  214.  
  215.     return (pixPos);
  216. }
  217.  
  218.  
  219.  
  220. /*****************************************************************************
  221.   FUNCTION :                      ui_utilPixToGrid
  222.  
  223.   PURPOSE  : transforms a pixel coordinate to a grid position 
  224.   RETURNS  : the corresponding grid position
  225.   NOTES    : to be consistent use always this routine to do the translation
  226. *****************************************************************************/
  227.  
  228. struct PosType
  229. ui_utilPixToGrid(struct Ui_DisplayType *displayPtr, struct PosType pixPos)
  230. {
  231.     struct PosType  gridPos;
  232.  
  233.     gridPos.x =  displayPtr->origin.x + (pixPos.x / displayPtr->gridSize);
  234.     gridPos.y =  displayPtr->origin.y + (pixPos.y / displayPtr->gridSize);
  235.     gridPos.z =  ui_actualZvalue;
  236. /*
  237.     gridPos.z =  0;
  238. */
  239.     return (gridPos);
  240. }
  241.  
  242.  
  243.  
  244. /*****************************************************************************
  245.   FUNCTION :                      ui_utilChangeFlag
  246.  
  247.   PURPOSE  : XOR operation of certain bits in a flag word
  248.   RETURNS  : alteration of certain bits in a flag word
  249. *****************************************************************************/
  250.  
  251.  
  252. void 
  253. ui_utilChangeFlag(FlagType *flag_word, FlagType flag)
  254. {
  255.     FlagType aux = *flag_word;
  256.  
  257.     if (ui_utilIsSet(aux, flag))
  258.     ui_utilResetFlag(aux,flag);
  259.     else
  260.     ui_utilSetFlag(aux,flag);
  261.     *flag_word = aux;
  262. }
  263.  
  264.  
  265.  
  266. /*****************************************************************************
  267.   FUNCTION :                        ui_utilSgnInt
  268.  
  269.   PURPOSE  : calculates the signum function for integer arguments
  270.   RETURNS  : sgn(x)  
  271. *****************************************************************************/
  272.  
  273.  
  274. int
  275. ui_utilSgnInt(int x)
  276. {
  277.     if (x > 0) return(1);
  278.     else
  279.     if (x < 0) return(-1);
  280.     else return(0);
  281. }
  282.  
  283.  
  284.  
  285. /*****************************************************************************
  286.   FUNCTION :                         ui_utilMax
  287.  
  288.   PURPOSE  : The varaibles x1 and x2 will be swapped, if x1 is not the maximum
  289.   RETURNS  : --- (side-effect)
  290.   NOTES    : Both arguments must be pointers to integers!
  291. *****************************************************************************/
  292.  
  293. void
  294. ui_utilMax(short int *x1Ptr, short int *x2Ptr)
  295. {
  296.     short h;
  297.  
  298.     if (*x1Ptr < *x2Ptr) { /* x1 is not the maximum, so swap */
  299.     h = *x1Ptr;  *x1Ptr = *x2Ptr;   *x2Ptr = h; 
  300.     } 
  301. }
  302.  
  303.  
  304.  
  305.  
  306. /*****************************************************************************
  307.   FUNCTION :                 ui_utilNormalizeRect
  308.  
  309.   PURPOSE  : Two pairs of coordinates are given describing a rectangle.
  310.              They will be reassigned, that (x1,y1) is the upper left and
  311.          (x2,y2) the lower right corner. 
  312.   RETURNS  : call by reference --> (x1Ptr,y1Ptr,x2Ptr,y2Ptr)
  313.   NOTES    : see ui_utilnormalize_coord()
  314. *****************************************************************************/
  315.  
  316. void
  317. ui_utilNormalizeRect(struct PosType *pixPos1Ptr, struct PosType *pixPos2Ptr)
  318. {
  319.     ui_utilMax(&(pixPos2Ptr->x), &(pixPos1Ptr->x));
  320.     ui_utilMax(&(pixPos2Ptr->y), &(pixPos1Ptr->y));
  321. }
  322.  
  323.  
  324.  
  325. /*****************************************************************************
  326.   FUNCTION :                       ui_utilPixUpperLeft
  327.  
  328.   PURPOSE  : calculates the pixel coordinates of the upper left corner of a
  329.              unit rectangle.
  330.   RETURNS  : coordinate in pixel
  331.   NOTES    : unit position are stored as raster (grid) positions
  332. *****************************************************************************/
  333.  
  334.  
  335. struct PosType
  336. ui_utilPixUpperLeft(struct Ui_DisplayType *displayPtr, struct PosType gridPos)
  337. {
  338.     struct PosType  pixPos;
  339.  
  340.     pixPos    = ui_utilGridToPix(displayPtr, gridPos);
  341.     pixPos.x -= ui_unitWidth/2;
  342.     pixPos.y -= ui_unitHeight/2;
  343.  
  344.     return(pixPos);
  345. }
  346.  
  347.  
  348.  
  349. /*****************************************************************************
  350.   FUNCTION :                  ui_utilPixLowerRight
  351.  
  352.   PURPOSE  : calculates the pixel coordinates of the lower right corner of a
  353.              unit rectangle.
  354.   RETURNS  : pixel coordinate
  355.   NOTES    : see above
  356. *****************************************************************************/
  357.  
  358. struct PosType
  359. ui_utilPixLowerRight(struct Ui_DisplayType *displayPtr, struct PosType gridPos, short int extent)
  360. {
  361.     struct PosType  pixPos;
  362.  
  363.  
  364.     pixPos    = ui_utilGridToPix(displayPtr, gridPos);
  365.     pixPos.x += ui_unitWidth  / 2 - 1 + extent;
  366.     pixPos.y += ui_unitHeight / 2 - 1 + extent;
  367.  
  368.     return (pixPos);
  369. }
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380. /* end of file */
  381. /* lines: 407 */
  382.